package com.lipeng.controller;

import com.lipeng.bean.User;
import com.lipeng.service.UserService;
import com.lipeng.utils.AddPic;
import com.lipeng.utils.AjaxResult;
import com.lipeng.utils.StrUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Created by dllo on 18/3/3.
 */
@Controller
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping(value = "userlist")
    public String userlist(){
        return "userlist";
    }

    @RequestMapping(value = "usersearch")
    public String usersearch(){
        return "usersearch";
    }

    @ResponseBody
    @RequestMapping(value = "alluser")
    public List<User> selectAllUser(){
        return userService.selectAllUser();
    }


    @RequestMapping(value = "adduser")
    public String adduser(MultipartFile file, HttpServletRequest request, Model model) throws IOException {
        String name = request.getParameter("name");
        String psw = request.getParameter("psw");
        String psw1 = request.getParameter("psw1");
        if (!psw.equals(psw1)){
            request.getSession().setAttribute("fail","fail");
            return "userlist";
        }
        if (!userService.selectUserByName(name).isEmpty()){
            request.getSession().setAttribute("fail","fail");
            return "userlist";
        }
        if (StrUtils.isNull(name) ||
                StrUtils.isNull(psw)) {
            request.getSession().setAttribute("fail","fail");
            return "userlist";
        }
        User user = new User();
        user.setUname(name);
        user.setUpsw(psw);
        user.setUsex(request.getParameter("sex"));
        user.setUtel(request.getParameter("tel"));
        user.setUintro(request.getParameter("intro"));
        user.setUadress(request.getParameter("adress"));
        user.setFile(file);
        user.setUstate(1);
        String sqlPath = AddPic.addPic(file,user);
        user.setUpic(sqlPath);
        int result = userService.addUser(user);
        model.addAttribute("user", user);
        if (result == 1){
            request.getSession().setAttribute("success","success");
        }else {
            request.getSession().setAttribute("fail","fail");
        }
        return "userlist";
    }

    @ResponseBody
    @RequestMapping(value = "searchname")
    public List<User> searchname(@Param("name")String name){
        System.out.println(name);
        List<User> userList = userService.selectUserByName(name);
        return userList;
    }

    @RequestMapping(value = "deluser")
    public String delUser(@RequestParam("uid")Integer uid,HttpServletRequest request){
        int result = userService.delUser(uid);
        if (result == 1){
            request.getSession().setAttribute("success","success");
        }else {
            request.getSession().setAttribute("fail","fail");
        }
        return "userlist";
    }

    @RequestMapping(value = "changestate")
    public String changeState(@RequestParam("ustate")int ustate,@RequestParam("uid")Integer uid, HttpServletRequest request){
        int state;
        if (ustate == 0){
            state = 1;
        }else {
            state = 0;
        }
        int result = userService.changeState(state,uid);
        if (result==1){
            request.getSession().setAttribute("success","success");
        }else {
            request.getSession().setAttribute("fail","fail");
        }
        return "redirect:userlist";
    }
}
